//
//  WHNewsViewController.m
//  WHEasyBizLink
//
//  Created by ShengMeng on 9/19/25.
//

#import "WHNewsViewController.h"
#import "SearchTitleView.h"
#import "SDAutoLayout.h"
#import "WHTdsModel.h"
#import "MJRefresh.h"

#define pagesize 80

@interface WHNewsViewController ()

@property (nonatomic,strong)UIBarButtonItem *rightItem;
@property(nonatomic,assign)CGFloat historyY;
@property (nonatomic,assign)int  mPageIndex;
@property(nonatomic,strong)NSString*searchString;

@end

@implementation WHNewsViewController
- (void)viewDidLoad {
    self.mPageIndex = 1;
    self.searchString = @"";
    [super viewDidLoad];
    //self.view.backgroundColor =[UIColor whiteColor];
    self.navigationController.navigationBar.topItem.title = @"消息";
   
}

#pragma mark - loadDatas
- (void)loadDatas{
    [super loadDatas];
    
}

#pragma mark - setupUI
- (void)setupUI{
    [super setupUI];
    //一个简单的super setupUI = createTableView
    //self.tableView.frame =CGRectMake(0,navHeight1+40,ScreenW,ScreenH-navHeight1-40);
    //self.navigationItem.rightBarButtonItem =self.rightItem;
    

    self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"enterprise_bg"].CGImage);
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.subviews.firstObject.alpha = 0;
    
    self.searchView.backgroundColor =[UIColor clearColor];
    self.searchView.searchBar.backgroundColor =[UIColor whiteColor];
    self.tableView.backgroundColor =[UIColor clearColor];

}

-(void)rightBarButtonItemClicked
{
    
}

#pragma mark 接收服务平台改变通知
-(void)notificationAction:(NSNotification *)notification{
    [self getComListWithisdropDown:NO];
}

#pragma mark - 查询公司列表
- (void)getComListWithisdropDown:(BOOL)isdropDown{
//    NSDictionary *tds = [WHTdsModel getSelTDSProxyList];
//    NSArray *allkey = [tds allKeys];
//    if (allkey.count==0) {
//        return;
//    }
//    
//    NSMutableDictionary *dic = [NSMutableDictionary new];
//    dic[@"currPage"]     = [NSString stringWithFormat:@"%d",self.mPageIndex];//self.comModel.id;
//    dic[@"perPageCount"] = [NSString stringWithFormat:@"%d",pagesize];
//    dic[@"udid"]         = kApp.userPhone;
//    dic[@"conditions"]   = self.searchString;
//    dic[@"proxy"]        = tds[@"url"];
//    dic[@"target"]       = tds[@"TDSid"];
//
//    [WHEnterpriseCompanyModel  qryListCompanyWithFromdata:dic succesefulBlock:^(id  _Nonnull responseObject) {
//        NSArray *dataList = (NSArray*)responseObject;
//        
//        if (dataList.count < pagesize){
//            //拿到当前的小于Request_count时，表示没有更多的数据了
//            [self setViewFooterWithHave:NO];
//        }else{
//            [self setViewFooterWithHave:YES];
//            // 拿到当前的不小于Request_count时，表还有更多的数据了
//            [self setViewFooterEndRefresh];
//        }
//        
//        NSMutableArray *mutabelArray;
//        if(isdropDown){
//            [self.datas addObjectsFromArray:dataList];
//            mutabelArray = self.datas;
//        }else{
//            mutabelArray = [NSMutableArray arrayWithArray:dataList];
//        }
//        self.datas =[NSMutableArray arrayWithArray:mutabelArray];
//    } andFailureBlock:^(NSString * _Nonnull error) {
//        NSLog(@"WHEnterpriseCompanyVC---查询单位列表失败：%@",error);
//        self.datas = [NSMutableArray new];
//    }];
}

#pragma mark 下拉刷新开始
- (void)dropDownRefresh
{
    if(!self.tableView.mj_footer) return;
    self.mPageIndex++;
    [self getComListWithisdropDown:YES];
}

- (void)setViewFooterWithHave:(BOOL) have{
    if(have){
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self dropDownRefresh];
        }];
    }else{
        self.tableView.mj_footer =nil;
    }
}

- (void)setViewFooterEndRefresh{
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - <searchViewDelegate>
-(void)searchTileViewTextFieldDidChange:(NSString*)textFieldString
{
    if (textFieldString.length==0)
    {
        self.mPageIndex = 1;
        self.searchString     = @"";
        [self.datas removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self getComListWithisdropDown:NO];
    }
}

//点击搜索调用
-(void)searchTileViewSearchButtonClickSearchString:(NSString *)searchString
{
    self.searchString = searchString;
    self.mPageIndex = 1;
    [self.datas removeAllObjects];
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
    [self getComListWithisdropDown:NO];
}

//设置滑动的判定范围
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (self.historyY+20<targetContentOffset->y)
    {
        [self setTabBarHidden:YES];
    }
    else if(self.historyY-20>targetContentOffset->y)
    {
        [self setTabBarHidden:NO];
    }
    self.historyY=targetContentOffset->y;
}

#pragma mark 隐藏显示tabbar
- (void)setTabBarHidden:(BOOL)hidden
{
    UIView *tab = self.tabBarController.view;
    CGRect  tabRect=self.tabBarController.tabBar.frame;
    if ([tab.subviews count] < 2) {
        return;
    }

    UIView *view;
    if ([[tab.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]) {
        view = [tab.subviews objectAtIndex:1];
    } else {
        view = [tab.subviews objectAtIndex:0];
    }

    if (hidden) {
        view.frame = tab.bounds;
        tabRect.origin.y=[[UIScreen mainScreen] bounds].size.height+self.tabBarController.tabBar.frame.size.height;
    } else {
        view.frame = CGRectMake(tab.bounds.origin.x, tab.bounds.origin.y, tab.bounds.size.width, tab.bounds.size.height);
        tabRect.origin.y=[[UIScreen mainScreen] bounds].size.height-self.tabBarController.tabBar.frame.size.height;
    }

    [UIView animateWithDuration:0.5f animations:^{
        self.tabBarController.tabBar.frame=tabRect;
    }completion:^(BOOL finished) {
    
    }];

}

-(UIBarButtonItem *)rightItem
{
    if (!_rightItem)
    {
        UIButton* rightBtn= [[UIButton alloc] initWithFrame:CGRectMake(0,0,25,25)];
      [rightBtn addTarget:self action:@selector(rightBarButtonItemClicked)forControlEvents:UIControlEventTouchUpInside];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"enterprise_add"] forState:UIControlStateNormal];
        _rightItem =[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    }
    return _rightItem;
}

@end


